// pages/resetPassword/resetPassword.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageType: '',
    // 注册新用户时需要手机号码
    mobile: '',
    password: '',
    password2: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      pageType: options.pagetype || '',
      mobile: options.mobile || '',
      code: options.code
    });
    wx.setNavigationBarTitle({
      title: options.pagetype === 'register' ? '设置密码' : options.pagetype === 'forgetPass' ? '重置密码' : '修改密码',
    })
  },

  passwordInput: function(e) {
    this.setData({
      password: e.detail.value
    })
  },

  passwordInput2: function(e) {
    this.setData({
      password2: e.detail.value
    })
  },

  submit: function() {
    var that = this;
    if (this.data.password === '' || this.data.password2 === '' || this.data.password !== this.data.password2) {
      app.showConfirm('提示', '请输入密码并检查两次输入的密码是否一致', false)
    } else {
      var data = {};
      if (this.data.pageType === 'forgetPass') {
        data = {
          point_url: app.server.agentApi.notLoginPassUpdate,
          // memberId: app.globalData.userInfo.id,
          password: this.data.password,
          mobile: that.data.mobile,
          verification: this.data.code
        };
      } else if (this.data.pageType === 'modifyPassword') {
        data = {
          point_url: app.server.agentApi.modifyPassword,
          memberId: app.globalData.userInfo.id,
          password: this.data.password,
        };
      } else {
        data = {
          point_url: app.server.agentApi.register,
          nickName: app.globalData.userInfo.nickName,
          avatarUrl: app.globalData.userInfo.avatarUrl,
          mobile: this.data.mobile,
          password: this.data.password
        };
      }
      app.server.getConnect(data).then(function(res) {
          wx.showToast({
            title: that.data.pageType === 'register' ? '注册成功' : '重置成功'
          })
          setTimeout(function () {
            if(app.globalData.config['tabbarType']==='custom'){
              wx.reLaunch({
                url: '/pages/agent/center/index/index',
              })
            } else {
              wx.switchTab({
                url: '/pages/agent/center/index/index',
              })
            }
          }, 1000)
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    app.pageOnloadInit(this, app);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})
